/**
 * Created by ACER on 2017/4/13.
 */
app.controller('personal-task-details', function ($scope, $http) {
    var taskid = sessionStorage.getItem('taskid');
    if (!taskid) {
        $.alert('找不到任务', function () {
            location.href = '#index';
        });
        return;
    }
    var token = localStorage.getItem('token');

    if (!token) {
        $.alert('请先登录', function () {
            location.href = '#login';
        });
        return;
    }
    $scope.taskId = JSON.parse(taskid);

    $http({
        method: 'get',
        url: app.netPath + '/tasks/detailedInfo',
        headers: {Authorization: token},
        params: {taskId: $scope.taskId}

    }).then(function (res) {


        //判断任务状态
        $scope.taskInfo = res.data.data;
        if ($scope.taskInfo.taskState == 0) {
            $scope.taskInfo.taskStateWords = '待完成';
        }
        else if ($scope.taskInfo.taskState == 1) {
            $scope.taskInfo.taskStateWords = '已完成';
        }
        else if ($scope.taskInfo.taskState == 2) {
            $scope.taskInfo.taskStateWords = '过期';
        }

        //截止时间
        if ($scope.taskInfo.taskDeadlineTime) {
            $scope.taskInfo.taskdeadline = parseInt(( new Date($scope.taskInfo.taskDeadlineTime).valueOf() - new Date().getTime()) / 86400000);
        }

        //
        console.log($scope.taskInfo.taskdeadline);
//头像图片
        var avatarImg = {};
        if (res.data.data.taskCreatorUser.userAvatarAddress) {
            avatarImg.imgAddress = app.netPath + '/resources/img/' + res.data.data.taskCreatorUser.userAvatarAddress;
            $scope.taskInfo.avatarImg = avatarImg;
        }
//任务图片

        $scope.taskInfo.imgAddresses = [];
        if (res.data.data.taskImageAddresses) {
            var taskImgs = JSON.parse(res.data.data.taskImageAddresses);
            if (taskImgs instanceof Array && taskImgs.length > 0) {
                for (var i = 0; i < taskImgs.length; i++) {
                    var img = app.netPath + '/resources/img/' + taskImgs[i];
                    if (taskImgs[i] == null || img == null || img == '')
                        continue;
                    var imgObject = {
                        imgSrc: img,
                        isLoad: false
                    };
                    $scope.taskInfo.imgAddresses.push(imgObject);
                }
            }
        }
        for (let p = 0; p < $scope.taskInfo.imgAddresses.length; p++) {
            var image = new Image();
            image.onload = function () {
                $scope.taskInfo.imgAddresses[p].isLoad = true;
                $scope.$digest();
            };
            image.src = $scope.taskInfo.imgAddresses[p].imgSrc;
        }


        //存储用户id

        $scope.getUserId = function () {
            sessionStorage.setItem('userid', $scope.userInfo.taskCreatorUser.userId);
            location.href = ''

        };


        //定位/距离

        $scope.latitude = $scope.taskInfo.taskPosition.positionX;
        $scope.longitude = $scope.taskInfo.taskPosition.positonY;
        if (parseInt(app.getGreatCircleDistance($scope.latitude, $scope.longitude)) > 1000)
            $scope.taskInfo.distanceKm = parseInt(app.getGreatCircleDistance($scope.latitude, $scope.longitude) / 1000);
        else {
            $scope.taskInfo.distancem = parseInt(app.getGreatCircleDistance($scope.latitude, $scope.longitude));
        }


        //删除任务
        $scope.deleteTask = function () {
            $http({
                method: 'post',
                url: app.netPath + '/tasks/details/delete',
                headers: {Authorization: token},
                params: {taskId: taskid}
            }).then(function (res) {
                if (!res.data.error) {
                    $.alert('删除成功', function () {
                        location.href = '#history-task';
                        return;
                    })
                }
            })
        };


        //完成任务
        $scope.finishTask = function () {


            $http({
                method: 'post',
                url: app.netPath + '/tasks/details/taskStateRevise',
                headers: {Authorization: token},
                params: {taskId: taskid, taskState: 1}
            }).then(function (res) {


                $scope.taskInfo.taskState = 1;
                if (!res.data.error) {
                    location.href = '#personal-task-details';
                    $.alert('任务完成', function () {

                        $scope.taskInfo = res.data.data;

                    })
                }
                else {
                    var errorCode = res.data.error;
                    switch (errorCode) {
                        case  200:
                            $.alert('修改任务状态失败');
                            return;
                    }
                }
            })
        }
    }, function () {
        $.alert('网络错误');
    })

});